<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-03-10
 * Time: 22:51
 */

namespace app\ops\controller;

class Goods extends Base

{

    function index()
    {
        $list = db('goods')->where(['sale'=> ['>', 0]])->field('sort, goods_id, name, cover, is_limit, num, sale')->order('sort desc, goods_id desc')->paginate(10);

        $this->assign('list', $list);
        return $this->fetch();
    }

    function edit($id = 0)
    {
        $post = request()->post();
        if($post)
        {
            unset($post['image']);
            $post['images'] = implode(',', $post['images']);
            $post['category_id'] = implode(',', $post['category_id']);
            $post['index_id'] = implode(',', $post['index_id']);
            if($post['goods_id'])
            {
                db('goods')->update($post);
            }else{
                unset($post['goods_id']);
                db('goods')->insert($post);
            }
            return success([]);
        }else{
            $data = db('goods')->where(['goods_id'=> $id])->find();
            $data['images'] = $data['images']?explode(',', $data['images']):[];
            $data['end_time'] = date('Y-m-d\TH:i:s', strtotime($data['end_time']));
            $category = db('goods_category')->where(['status'=> 1])->select();
            $index = db('category_index')->where(['status'=> 1])->select();
//            dump($data);
            $this->assign('data', $data);
            $this->assign('category', $category);
            $this->assign('index', $index);
            return $this->fetch();
        }
    }


    function delete()
    {
        $id = request()->post('id');
        db('goods')->where(['goods_id'=> $id])->delete();
        return success([]);
    }

    function detail()
    {
        $id = request()->param('id');
        $data = db('goods')->where(['goods_id'=> $id])->find();
        $data['images'] = $data['images']?explode(',', $data['images']):[];
//        dump($data);
        $coupon = db('coupon c')
            ->join('company m', 'c.uid = m.id')
            ->where(['c.coupon_id'=> ['in', $data['coupon_id']]])
            ->field('c.name, m.name as company_name, c.coupon_id, c.start_time, c.end_time, c.type, c.value, c.num, c.create_time')
            ->select();
        $this->assign('data', $data);
        $this->assign('coupon', $coupon);
        return $this->fetch();
    }

    function upload()
    {
        $file = request()->file('imgFile');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->validate(['size'=>2097152])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                // 成功上传后 获取上传信息
                // 输出 jpg
                $url = DS . 'uploads' . DS . $info->getSaveName();
            } else {
                // 上传失败获取错误信息
                return json_encode(['error'=> 1, 'message'=> '上传失败']);
            }
        } else {
            return fail();
        }

        return json_encode(['error'=> 0, 'url' => config('host') . $url]);

    }

    function pull()
    {
        $id = request()->post('id');
        db('goods')->where(['goods_id'=> $id])->update(['sale'=> request()->param('sale')]);
        return success([]);
    }
}